﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OF.Web.admin
{
    public partial class dishcart : System.Web.UI.Page
    {
        string strwhere = "";
        BLL.Dish.DishCart dishcartBll = new BLL.Dish.DishCart();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        public void Bind()
        {
            strwhere = " sellerid =" + WebUtils.GetId();
            OFPager.RecordCount = dishcartBll.GetRecordCount(strwhere);
            RepDishCart.DataSource = dishcartBll.GetListByPages(strwhere, "", OFPager.CurrentPageIndex, OFPager.PageSize);
            RepDishCart.DataBind();

            if (RepDishCart.Items.Count == 0)
                Nodata.Visible = true;
            else
                Nodata.Visible = false;
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string cartname = txtCartName.Text.Trim();
            int state = Convert.ToInt32(ddlState.SelectedValue);
            Model.DishCarts dishcartModel = new Model.DishCarts();
            dishcartModel.catname = cartname;
            dishcartModel.parentid = 1;//默认值
            dishcartModel.sellerid = WebUtils.GetId();
            dishcartModel.state = state;//默认值
              //编辑
            if (!string.IsNullOrWhiteSpace(hfid.Value))
            {
                dishcartModel.id = Convert.ToInt32(hfid.Value);
            }
            //判断名称是否重复
            if (dishcartBll.GetRecordCount(" id!=" + dishcartModel.id + " and catname='" + cartname + "' and sellerid=" + dishcartModel.sellerid + "") > 0)
            {
                Common.MessageBox.Show(this, "名称已存在");
                return;
            }
            bool result = false;
            //编辑
            if (dishcartModel.id>0)
            {
                result = dishcartBll.Update(dishcartModel);
            }
            else
            {
                result = dishcartBll.Add(dishcartModel) > 0 ? true : false;
            }

            if (result)
            {
                //更新缓存
                OF.BLL.Addr.AreaCache cache = new OF.BLL.Addr.AreaCache();
                cache.UpdateCache(OF.Common.Enmus.CACHE_TYPE.C_DISHCAT);

                Common.MessageBox.Show(this, "保存成功");
                Bind();
            }
            else
            {
                Common.MessageBox.Show(this, "保存失败,请稍后重试");
            }
        }
    }
}